package com.karry.yl.web.elder;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Bed;
import com.karry.yl.domain.baseinfo.Building;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.domain.elder.CheckInRecords;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.service.baseinfo.BedService;
import com.karry.yl.service.baseinfo.BuildingService;
import com.karry.yl.service.baseinfo.RoomService;
import com.karry.yl.service.elder.CheckInRecordsService;
import com.karry.yl.service.elder.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 入住记录Controller
 */

@Controller
@RequestMapping("/checkinrecords")
public class CheckInRecordsController extends SuperController {
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private CheckInRecordsService checkInRecordsService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;

    @RequestMapping
    public String listUI(){
        return "elder/checkinrecords/list";
    }

    @RequestMapping("/add")
    public String addUI(Long id,ModelMap map){
        Elderly elderly = elderlyService.selectOne(new EntityWrapper<Elderly>().eq("id", id).eq("orgId", currentUser.getOrgId()).eq("deleted",false));
        map.put("elderly",elderly);
        List<Building> buildings = buildingService.selectList(new EntityWrapper<Building>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("status",Building.STATUS_TY));
        map.put("buildings",buildings);
        return "elder/checkinrecords/add";
    }


    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<CheckInRecords> checkInRecordsPage = checkInRecordsService.selectListPage(new Page<CheckInRecords>(page, limit),currentUser.getOrgId());
        return new LayTableResponse(checkInRecordsPage.getRecords(),checkInRecordsPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(CheckInRecords checkInRecords){
        checkInRecords.setOrgId(currentUser.getOrgId());
        checkInRecords.setOrgName(currOrganization.getName());
        checkInRecordsService.save(checkInRecords);
        return "redirect:/gbElderly/elist";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResponse update(CheckInRecords checkInRecords){
        checkInRecords.setOrgId(currentUser.getOrgId());
        checkInRecords.setOrgName(currOrganization.getName());
        if(!StringUtils.isEmpty(checkInRecords.getBuildingId())){
            Building building = buildingService.selectById(checkInRecords.getBuildingId());
            checkInRecords.setBuildingNo(building.getName());
        }
        if(!StringUtils.isEmpty(checkInRecords.getRoomId())){
            Room room = roomService.selectById(checkInRecords.getRoomId());
            checkInRecords.setRoomNo(room.getCode());
        }
        checkInRecordsService.checkInUpdate(checkInRecords,currentUser.getOrgId());
        return new JsonResponse(200,"跟新入住记录成功");
    }


    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        checkInRecordsService.deleteBatchIds(ids);
        return "redirect:/checkinrecords";
    }

    @RequestMapping("/rooms")
    @ResponseBody
    public JsonResponse getRoomByBuildingId(Long buildingId){
        List<Room> rooms = roomService.selectList(new EntityWrapper<Room>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("buildingId", buildingId).eq("status",Room.STATUS_KY));
        return JsonResponse.success("rooms",rooms);
    }

    @RequestMapping("/beds")
    @ResponseBody
    public JsonResponse getBedByRoomId(Long roomId){
        List<Bed> rooms = bedService.selectList(new EntityWrapper<Bed>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("roomId", roomId).eq("status",Bed.STATUS_KX));
        return JsonResponse.success("rooms",rooms);
    }

    @RequestMapping("/getElderly")
    @ResponseBody
    public JsonResponse getElderly(String buildingNo, String roomNo){
        List<CheckInRecords> records = checkInRecordsService.selectList(new EntityWrapper<CheckInRecords>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("buildingNo", buildingNo).eq("roomNo",roomNo));
        return JsonResponse.success("records",records);
    }
}
